package com.ft.oa.service.industryService.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ft.oa.domain.customer.Customer;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.domain.industrys.Industry;
import com.ft.oa.domain.industrys.IndustryContributor;
import com.ft.oa.domain.industrys.IndustryCustomerRelation;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.mapper.customer.CustomerMapper;
import com.ft.oa.mapper.industrysMapper.IndustryCustomerRelationMapper;
import com.ft.oa.mapper.industrysMapper.IndustryMapper;
import com.ft.oa.security.constant.SecurityConstant;
import com.ft.oa.service.industryService.IIndustryCustomerRelationService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 行业客户Service业务层处理
 * 
 * @author ft
 * @date 2021-12-30
 */
@Service
public class IndustryCustomerRelationServiceImpl implements IIndustryCustomerRelationService {
    @Resource
    private IndustryCustomerRelationMapper industryCustomerRelationMapper;
    @Resource
    private IndustryMapper industryMapper;
    @Resource
    private CustomerMapper customerMapper;
    // 查询
    @Override
    public int customernum() {
        int custnum = 0;
        // 获取当前用户信息
        SecurityUser currentUser = SecurityUtils.getCurrentUser();
        // 获取当前用户名称用作展示售前项目管理
        Long creator = currentUser.getId();
        //获取用户权限 如果是admin 则是管理员 可以查看到全部
        QueryWrapper<Industry> wrapper = new QueryWrapper<>();
        Set<String> roles = currentUser.getRoles();
        for (String role:roles) {
            if(role.equals(SecurityConstant.SUPER_ROLE)){
                wrapper.eq("delldust","0");
            }else{
                wrapper.eq("delldust","0");
                wrapper.eq("responsibility_user",creator);
            }
        }
        List<Industry> industries = industryMapper.selectList(wrapper);
        for (Industry indus: industries){
            Long id = indus.getId();
            QueryWrapper<IndustryCustomerRelation> wrappercustomer = new QueryWrapper<>();
            wrappercustomer.eq("industry_id",id);
            wrappercustomer.eq("dellpeople","0");
            int num = industryCustomerRelationMapper.selectCount(wrappercustomer);
            custnum+=num;
        }
        return custnum;
    }

    /**
     * 根据行业id查询目标客户人
     * @param industryid
     * @return
     */
    @Override
    public List<Customer> targetpeople(int industryid,String industryName) {
        QueryWrapper<IndustryCustomerRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("industry_id",industryid);
        wrapper.eq("dellpeople","0");
        List peoplelist = new ArrayList<>();
        List<IndustryCustomerRelation> industryCustomerRelations = industryCustomerRelationMapper.selectList(wrapper);
        industryCustomerRelations.forEach(relationList ->{
            HashMap<String,Object> targetcustomersMap = new HashMap<>();
            Customer customer = customerMapper.selectById(relationList.getCustomerCompanyId());
            // 客户对接人
            targetcustomersMap.put("CustomerName",customer.getCustomerName());
            //创建人姓名
            targetcustomersMap.put("creatorName",customer.getCreatorName());
            //公司名称
            targetcustomersMap.put("name",customer.getName());
            //客户主表id
            targetcustomersMap.put("id",customer.getId());
            targetcustomersMap.put("industryid",industryid);
            targetcustomersMap.put("targetpeopleid",relationList.getId());
            targetcustomersMap.put("industryName",industryName);
            peoplelist.add(targetcustomersMap);
        });
        return peoplelist;
    }

    /**
     * 删除目标客户
     * @param targetpeopleid
     * @return
     */
    @Override
    public int delltargetpeople(String targetpeopleid) {
        UpdateWrapper<IndustryCustomerRelation> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",targetpeopleid);
        wrapper.set("dellpeople","1");
        return industryCustomerRelationMapper.update(null,wrapper);
    }

    /**
     * 新增目标客户
     * @param industryCustomerRelation
     * @return
     */
    @Override
    public int addtargetpeople(IndustryCustomerRelation industryCustomerRelation) {
        int succes = 0;
        List<String> peopleList = industryCustomerRelation.getRelationList();
        for (String list:peopleList) {
            IndustryCustomerRelation contributor = new IndustryCustomerRelation();
            String industry = industryCustomerRelation.getIndustryId();
            QueryWrapper<IndustryCustomerRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("industry_id",industry);
            wrapper.eq("customer_company_id",list);
            wrapper.eq("dellpeople","0");
            if (industryCustomerRelationMapper.selectList(wrapper).size()!=0){
                contributor.setDellpeople("0");
                industryCustomerRelationMapper.update(contributor,wrapper);
            }else{
                contributor.setCustomerCompanyId(list);
                contributor.setIndustryId(industry);
                int inserts = industryCustomerRelationMapper.insert(contributor);
                succes+=inserts;
            }
        }
        return succes;
    }
}
